﻿#include "dialog.h"

TDialog::TDialog(QWidget *parent) : QDialog(parent)
{
    this->max = false;
    this->mousePressed = false;

    this->InitTitle();

    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_DeleteOnClose);        //设置窗体关闭时自动释放内存
    this->setSizeGripEnabled(true);                  //设置可以拖拽大小
    this->setModal(true);

    this->setTitle("弹出窗口");
    this->setObjectName("dialagMain");
}

TDialog::~TDialog()
{
    delete labIco;
    delete labTitle;
    delete butMenuMin;
    delete butMenuMax;
    delete butMenuClose;
}

void TDialog::InitTitle(){

    labIco = new QLabel();
    labIco->setObjectName("lab_Title");
    labIco->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    labIco->setText(QChar(0xe62b));

    labTitle = new QLabel();
    labTitle->setObjectName("lab_Title");
    labTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    labTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    labTitle->setText("自定义UI");

    butMenuMin = new QPushButton();
    butMenuMin->setObjectName("btnMenu_Min");
    butMenuMin->setToolTip("最小化");
    butMenuMin->setText(QChar(0xe731));
    butMenuMin->hide();

    butMenuMax = new QPushButton();
    butMenuMax->setObjectName("btnMenu_Max");
    butMenuMax->setToolTip("最大化");
    butMenuMax->setText(QChar(0xe7ea)); //68d

    butMenuClose = new QPushButton();
    butMenuClose->setObjectName("btnMenu_Close");
    butMenuClose->setToolTip("关闭");
    butMenuClose->setText(QChar(0xe666));


    QHBoxLayout *titleLayout = new QHBoxLayout();
    titleLayout->setMargin(0);
    titleLayout->setSpacing(0);
    titleLayout->addWidget(labIco);
    titleLayout->addWidget(labTitle);
    titleLayout->addWidget(butMenuMin);
    titleLayout->addWidget(butMenuMax);
    titleLayout->addWidget(butMenuClose);

    QWidget *titleWidget = new QWidget();
    titleWidget->setMaximumHeight(30);
    titleWidget->setObjectName("widget_Title");
    titleWidget->setLayout(titleLayout);

    mainLayout = new QVBoxLayout();
    QWidget *mainWidget = new QWidget();
    mainWidget->setLayout(mainLayout);

    QVBoxLayout *layout = new QVBoxLayout();
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(titleWidget);
    layout->addWidget(mainWidget);
    this->setLayout(layout);

    //安装事件监听器,让标题栏识别鼠标双击
    labTitle->installEventFilter(this);

    connect(butMenuMin, SIGNAL(clicked()), this, SLOT(OnBtnMenuMinClicked()));
    connect(butMenuMax, SIGNAL(clicked()), this, SLOT(OnBtnMenuMaxClicked()));
    connect(butMenuClose, SIGNAL(clicked()), this, SLOT(OnBtnMenuCloseClicked()));
}

void TDialog::setMaxHide(){
    this->butMenuMin->hide();
    this->butMenuMax->hide();
    this->setSizeGripEnabled(false);                  //设置可以拖拽大小
}
void TDialog::setTitle(QString title){
    this->labTitle->setText(title);
    this->setWindowTitle(title);
}
void TDialog::addLayout(QLayout *layout){
    this->mainLayout->addLayout(layout);
}
void TDialog::addWidget(QWidget *widget){
    this->mainLayout->addWidget(widget);
}


bool TDialog::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonDblClick) {
        this->OnBtnMenuMaxClicked();
        return true;
    }
    return QObject::eventFilter(obj, event);
}

void TDialog::mouseMoveEvent(QMouseEvent *e)
{
    if (mousePressed && (e->buttons() && Qt::LeftButton)) {
        if(max){
            return;
        }
        this->move(e->globalPos() - mousePoint);
        e->accept();
    }
}

void TDialog::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        mousePressed = true;
        mousePoint = e->globalPos() - this->pos();
        e->accept();
    }
}

void TDialog::mouseReleaseEvent(QMouseEvent *)
{
    mousePressed = false;
}


void TDialog::OnBtnMenuCloseClicked()
{
    this->close();
}

void TDialog::OnBtnMenuMaxClicked()
{

    if (max) {
        this->showNormal();
        this->butMenuMax->setText(QChar(0xe7ea));
        this->butMenuMax->setToolTip("最大化");
    } else {
        this->showMaximized();
        this->butMenuMax->setText(QChar(0xe68d));
        this->butMenuMax->setToolTip("还原");
    }
    max = !max;
}

void TDialog::OnBtnMenuMinClicked()
{
    this->showMinimized();
}
